var express = require('express');
var router = express.Router();
var csfsCon = require('../controllers/csfs');
// var sea = require("../common/sea")
var sqlOrm = require('../common/sqlModel');
const utils = require("../common/utils")

/* GET home page. */

let getBody = (req,res,cb)=>{
    console.log("getBody .method is",req.method)
    var url = req.path;
    console.log("url is",url)
    if (req.method == "GET"){
        body = req.query
        if(!body){
            console.error("{code:500,msg:参数不足}",req.body)
            var resultString = JSON.stringify({code:500,msg:"参数不足"});
            res.end(resultString)
            return
        }
    }else{
        body = req.body
        if (!req.body) {
            console.error("{code:500,msg:参数不足}",req.body)
            var resultString = JSON.stringify({code:500,msg:"参数不足"});
            res.end(resultString)
            return
        }
    }
    console.log("body",body)
    // if (url != "/api/login/account" ){
    //     let {session,id} = body.session
    //     let isLogin = utils.chenkLogin(id,session)
    //     console.log("isLogin",isLogin)
    //     if(!isLogin){
    //         var resultString = JSON.stringify({code:500,msg:"没有登录"});
    //         res.end(resultString)
    //     }
    // }


    console.log("URL is",url)
    console.log("客户端传入的数据是",body)
    return cb(body)
}

let retClient = (req,result)=>{
    console.log("返回客户端的数据是",result)
    console.log("URL is",req.url)
    return result
}



//登录
router.all('/api/login/account',(req, res, next) =>getBody(req, res,function(body) {
    csfsCon.login(req, res,body,function(result) {
        let resultString =  retClient(req,result)
        console.log("xxxxxx",resultString)
        res.json(resultString)
    })
}));

router.all('/loginOut',(req, res, next) =>getBody(req, res,function(body) {
    csfsCon.loginOut(req, res,body,function(result) {
        let resultString =  retClient(req,result)
        console.log("xxxxxx",resultString)
        res.json(resultString)
    })
}));

router.all('/api/fake_chart_data',(req, res, next) =>getBody(req, res,function(body) {
    csfsCon.TableInfo(req, res,body,function(result) {
        let resultString =  retClient(req,result)
        console.log("xxxxxx",resultString)
        res.json(resultString)
    })
}));


router.all('/api/currentUser',(req, res, next) =>getBody(req, res,function(body) {
    csfsCon.currentUse(req, res,body,function(result) {
        let resultString =  retClient(req,result)
        console.log("xxxxxx",resultString)
        res.json(resultString)
    })
}));

// 警告
router.all('/api/traceWarn',(req, res, next) =>getBody(req, res,function(body) {
    csfsCon.traceWarn(req, res,body,function(result) {
        let resultString =  retClient(req,result)
        console.log("xxxxxx",resultString)
        res.json(resultString)
    })
}));

// getHeroNum
router.all('/api/getHeroNum',(req, res, next) =>getBody(req, res,function(body) {
    csfsCon.getHeroNum(req, res,body,function(result) {
        let resultString =  retClient(req,result)
        console.log("xxxxxx",resultString)
        res.json(resultString)
    })
}));
// 角色基本信息
router.all('/api/rule',(req, res, next) =>getBody(req, res,function(body) {
    csfsCon.accountInfo(req, res,body,function(result) {
        let resultString =  retClient(req,result)
        res.json(resultString)
    })
}));

// 角色基本信息
router.all('/api/updateAccountBan',(req, res, next) =>getBody(req, res,function(body) {
    csfsCon.upateAccountInfo(req, res,body,function(result) {
        let resultString =  retClient(req,result)
        console.log("xxxxxx",resultString)
        res.json(resultString)
    })
}));

// 获取异常订单列表
router.all('/api/fake_list',(req, res, next) =>getBody(req, res,function(body) {
    csfsCon.unDotrace(req, res,body,function(result) {
        let resultString =  retClient(req,result)
        console.log("xxxxxx",resultString)
        res.json(resultString)
    })
}));
// 修改订单状态
router.all('/api/updateTraceStatus',(req, res, next) =>getBody(req, res,function(body) {
    csfsCon.updateTraceStatus(req, res,body,function(result) {
        let resultString =  retClient(req,result)
        console.log("xxxxxx",resultString)
        res.json(resultString)
    })
}));


//
router.all('/api/updateTicketStatus',(req, res, next) =>getBody(req, res,function(body) {
    csfsCon.updateTicketStatus(req, res,body,function(result) {
        let resultString =  retClient(req,result)
        console.log("xxxxxx",resultString)
        res.json(resultString)
    })
}));


//
router.all('/api/getTicketStatus',(req, res, next) =>getBody(req, res,function(body) {
    csfsCon.getTicketStatus(req, res,body,function(result) {
        let resultString =  retClient(req,result)
        console.log("xxxxxx",resultString)
        res.json(resultString)
    })
}));

//
router.all('/api/getpAccountList',(req, res, next) =>getBody(req, res,function(body) {
    csfsCon.getPaccount(req, res,body,function(result) {
        let resultString =  retClient(req,result)
        console.log("xxxxxx",resultString)
        res.json(resultString)
    })
}));

//
router.all('/api/newPAccount',(req, res, next) =>getBody(req, res,function(body) {
    csfsCon.addPaccount(req, res,body,function(result) {
        let resultString =  retClient(req,result)
        console.log("xxxxxx",resultString)
        res.json(resultString)
    })
}));

//
router.all('/api/sendItem',(req, res, next) =>getBody(req, res,function(body) {
    csfsCon.sendItem(req, res,body,function(result) {
        let resultString =  retClient(req,result)
        console.log("xxxxxx",resultString)
        res.json(resultString)
    })
}));


//修改用户阀值
router.all('/api/submitmaxValue',(req, res, next) =>getBody(req, res,function(body) {
    csfsCon.submitmaxValue(req, res,body,function(result) {
        let resultString =  retClient(req,result)
        console.log("xxxxxx",resultString)
        res.json(resultString)
    })
}));


//修改账号密码
router.all('/api/updatePwd',(req, res, next) =>getBody(req, res,function(body) {
    csfsCon.updatePwd(req, res,body,function(result) {
        let resultString =  retClient(req,result)
        console.log("xxxxxx",resultString)
        res.json(resultString)
    })
}));


//发送短信
router.all('/api/sendMsg',(req, res, next) =>getBody(req, res,function(body) {
    csfsCon.sendMsg(req, res,body,function(result) {
        let resultString =  retClient(req,result)
        console.log("xxxxxx",resultString)
        res.json(resultString)
    })
}));




module.exports = router;
